﻿package  {
	
	import com.as3toolkit.events.KeyboarderEvent;
	import com.as3toolkit.ui.Keyboarder;
	import flash.events.KeyboardEvent;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.ui.Keyboard;
	import flash.geom.Point;
	import com.greensock.TweenMax;
	import com.greensock.easing.*;
	
	public class player extends MovieClip {
		
		var kb:Keyboarder;
		public var obst:Vector.<obstacle>;
		
		public function player() {
			addEventListener(Event.ENTER_FRAME, update);
			stage.addEventListener(KeyboardEvent.KEY_UP, fireMagic);
			
		}
		
		public function update(e:Event)
		{
			 var radians:Number = Math.atan2(parent.mouseY - y, parent.mouseX - x);
            var degrees:Number = radians / (Math.PI /180)+ 90;
            this.rotation = degrees;
			
			doKeys();
		}
		
		public function checkCollisions(): Boolean
		{
			if(obst != null)
			{
				for(var i:int = 0; i < obst.length; i++)
				{
					if(c.hitTestObject(obst[i]))
					   {
						   return true;
					   }
				}
			}
			
			return false;
		}
		
		public function doKeys():void
		{
			if(Keyboarder.keyIsDown(Keyboard.W)
			   || Keyboarder.keyIsDown(Keyboard.S)
			   || Keyboarder.keyIsDown(Keyboard.A)
			   || Keyboarder.keyIsDown(Keyboard.D))
			{
				Feet.play();
				if (Keyboarder.keyIsDown(Keyboard.W))
				{
					if(magicArm.hand.currentFrameLabel != "idle")
					{
						y-=2;
						
						if(checkCollisions())
						{
						  y+= 2;
						}
					}
					else
					{
					y-=5;
					
						if(checkCollisions())
						{
							y+=5;
						}
					}
					
					if(y < 0)
					{
						y = 0;
					}
					
				}
			
				if(Keyboarder.keyIsDown(Keyboard.S))
				{
					if(magicArm.hand.currentFrameLabel != "idle")
					{
						y+=3;
						
						if(checkCollisions())
						{
							y-=3;
						}
					}
					else
					{
					y+=7;
					
						if(checkCollisions())
						{
							y-=7;
						}
					}
					
					
					if(y > (parent as Map).base.height - 20)
					{
						y = (parent as Map).base.height - 20;
					}
				}
			
				if(Keyboarder.keyIsDown(Keyboard.A))
				{
					if(magicArm.hand.currentFrameLabel != "idle")
					{
						x-=3;
						
						if(checkCollisions())
						{
							x+=3;
						}
					}
					else
					{
						x-=7;
						
						if(checkCollisions())
						{
							x+=7;
						}
					}
					if(x < 0)
					{
						x = 0;
					}
				}
			
				if(Keyboarder.keyIsDown(Keyboard.D))
				{
					if(magicArm.hand.currentFrameLabel != "idle")
					{
						x+=3;
						
						if(checkCollisions())
						{
							x-=3;
						}
					}
					else
					{
						x+=7;
						
						if(checkCollisions())
						{
							x-=7;
						}
					}
					
		
					if(x > (parent as Map).base.width - 20)
					{
						x = (parent as Map).base.width - 20;
					}
				}
			}
			else
			{
				Feet.stop();
			}
			
			if(Keyboarder.keyIsDown(Keyboard.SPACE))
			{
				if( meleeArm.currentFrameLabel == "idle" && magicArm.currentFrameLabel == "Idle"
				    && magicArm.hand.currentFrameLabel == "idle")
				{
					meleeArm.play();
				}
			}
			
			if(Keyboarder.keyIsDown(Keyboard.SHIFT))
			{
				if(meleeArm.currentFrameLabel == "idle" && magicArm.currentFrameLabel == "Idle")
				{
					if(magicArm.hand.currentFrameLabel == "idle")
					{
					magicArm.hand.gotoAndPlay("First");
					}
					
					
					if(Keyboarder.holdDuration(Keyboard.SHIFT) > 1500
					   && (magicArm.hand.currentFrame >= 2 
						   && magicArm.hand.currentFrame <= 13))
					{
						magicArm.hand.gotoAndPlay("Second");
					}
					else if(Keyboarder.holdDuration(Keyboard.SHIFT) > 3000
					   && (magicArm.hand.currentFrame >= 14 
						   && magicArm.hand.currentFrame <= 25))
					{
						magicArm.hand.gotoAndPlay("Third");
					}
				}
			}
		}
		
		public function fireMagic(e:KeyboardEvent):void
		{
			if(magicArm.hand.currentFrameLabel != "idle")
			{
				if(e.keyCode == Keyboard.SHIFT)
				{
					var boom:playerBlast = new playerBlast();
					var p:Point = new Point(-65.55, -5.90);
					p = localToGlobal(p);
					parent.addChild(boom);
					(parent as Map).pb = boom;
					if(magicArm.hand.currentFrame >= 14 &&
					   magicArm.hand.currentFrame <= 25)
					   {
						   boom.gotoAndPlay("Second");
					   }
					else if(magicArm.hand.currentFrame > 25)
					{
						boom.gotoAndPlay("Third");
					}
					boom.x = p.x - parent.x; boom.y = p.y - parent.y;
					boom.blast(parent.mouseX, parent.mouseY);
					magicArm.hand.gotoAndStop("idle");
					magicArm.play();
				}
			}
		}
	}
	
}
